package web.admin.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.Droga;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.CriteriaDrug;
import services.DrogaService;

import java.util.ArrayList;
import java.util.List;

@Namespace("/Admin/Drug")
public class DrogaController extends ActionSupport implements ModelDriven {

    private DrogaService drogaService;
    private Droga droga = new Droga();
    private List<Droga> list = new ArrayList<Droga>(0);;
    private Integer drogaId;
    private String errorMessage;

    @Autowired
    public DrogaController(DrogaService drogaService){
        this.drogaService= drogaService;
    }

    @Override
    public Object getModel() {
        return droga;
    }

    @Action(value="main", results={
            @Result(name="success", location="/admin.droga.tiles", type="tiles")
    })
    public String execute() {
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/admin.droga.tiles", type="tiles")
    })
    public String list() {
        CriteriaDrug crS = new CriteriaDrug(droga);
        list = drogaService.list(crS);
        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/admin.droga.create.tiles", type="tiles")
    })
    public String createDrug() {

        if(drogaId != null){
            droga = drogaService.findById(drogaId);
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction"),
            @Result(name="error",location="/admin.droga.create.tiles", type="tiles")
    })
    public String saveDrug() {
        if( drogaService.findByDrugNumber(droga.getDrugNumber()) != null ){
            errorMessage = new String("A drug with the number " + droga.getDrugNumber() + " already exists.");
            return ERROR;
        }
        if ( drogaService.findByDrugName(droga.getDrugName()) != null){
            errorMessage = new String("A drug with the name " + droga.getDrugName() + " already exists.");
            return ERROR;
        }
        drogaService.save(droga);
        return SUCCESS;
    }

    public List<Droga> getList() {
        return list;
    }

    public void setList(List<Droga> list) {
        this.list = list;
    }

    public Droga getDroga() {
        return droga;
    }

    public void setDroga(Droga droga) {
        this.droga = droga;
    }

    public Integer getDrogaId() {
        return drogaId;
    }

    public void setDrogaId(Integer drogaId) {
        this.drogaId = drogaId;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}
